// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.portlet.services.response;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.portlet.ResourceURL;

import org.apache.tapestry5.services.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Adapts {@link javax.portlet.PortletResponse} as
 * {@link org.apache.tapestry.web.WebResponse}.
 * 
 * @author Howard M. Lewis Ship
 * @since 4.0
 */

// TODO: rename to ActionResponseImpl
public class PortletActionResponse extends AbstractPortletResponse {

    private final Logger _logger     = LoggerFactory.getLogger(PortletActionResponse.class);

    private boolean      _isCommited = false;

    public PortletActionResponse(PortletResponse portletResponse, Request portletRequest) {
        super(portletResponse, portletRequest);
    }

    @Override
    public PortletURL createRenderURL() {
        throw new UnsupportedOperationException("ActionResponse can not create URLs!");
    }

    @Override
    public PortletURL createActionURL() {
        throw new UnsupportedOperationException("ActionResponse can not create URLs!");
    }

    @Override
    public ResourceURL createResourceURL() {
        throw new UnsupportedOperationException("ActionResponse can not create URLs!");
    }

    @Override
    public boolean isCommitted() {
        this._logger.info("Commited: " + this._isCommited);

        return this._isCommited;
    }

    @Override
    public PrintWriter getPrintWriter(String contentType) throws IOException {
        throw new UnsupportedOperationException("ActionResponse is not supposed to generate output!");

        //      _logger.error(PortletServicesMessages.unsupportedMethod("getPrintWriter"));
        //      return null;
        //      LiferayPortletResponse liferayResponse = (LiferayPortletResponse) _response;
        //      return liferayResponse.getHttpServletResponse().getWriter();

        //      HttpServletResponse servletResponse = ((LiferayPortletResponse) _response).getHttpServletResponse();
        //      _logger.info("getPrintWriter " + contentType + " " + servletResponse.getCharacterEncoding());
        //      servletResponse.setContentType(contentType);
        //      OutputStream os = servletResponse.getOutputStream();
        //      Writer w = new OutputStreamWriter(os, servletResponse.getCharacterEncoding());
        //      return new PrintWriter(new BufferedWriter(w));
        //        this._logger.info("getPrintWriter");
        //
        //        if (this._request == null) {
        //            this._logger.info("REQUEST IS NULL");
        //            return null;
        //        }
        //
        //        if (this._request.isXHR()) {
        //            //            _logger.info("Request is XHR IN RESPONSE TOO");
        //            // TODO: Content Type Header + Encoding setzen (?)
        //            this._logger.info("XHR Content-Type: " + contentType);
        //            LiferayPortletResponse liferayResponse = (LiferayPortletResponse) this._response;
        //            liferayResponse.setHeader("Content-Type", contentType);
        //            this._logger.info("Response Content-Type: " + liferayResponse.getHttpServletResponse().getContentType());
        //            return liferayResponse.getHttpServletResponse().getWriter();
        //        }
        //        else {
        //            // 
        //            //            _logger.info("Request is not XHR IN RESPONSE");
        //            return null;
        //        }
    }

    @Override
    public OutputStream getOutputStream(String contentType) throws IOException {
        //      _logger.error(PortletServicesMessages.unsupportedMethod("getOutputStream"));
        this._logger.info("getOutputStream: " + contentType);
        this._isCommited = true;

        /** TODO:
         *  Liferay tries to write to the PrintWriter after we aquired the OutputStream,
         *  causing an IllegalStateException: Cannot obtain Writer because OutputStream is already in use
         *  
         *  we should handle these requests by using Resource requests 
         */
        throw new UnsupportedOperationException("ActionResponse is not supposed to generate output!");

        //        LiferayPortletResponse liferayResponse = (LiferayPortletResponse) this._response;
        //        HttpServletResponse httpServletResponse = liferayResponse.getHttpServletResponse();
        //
        //        httpServletResponse.setContentType(contentType);
        //        return httpServletResponse.getOutputStream();
    }
}
